﻿#include "dlglevel.h"
#include "ui_dlglevel.h"

#include <QDebug>
#include <QTimer>
#include <QMessageBox>

DlgLevel::DlgLevel(QSettings* settings, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgLevel),
    m_settings(settings)
{
    ui->setupUi(this);

    // 初始化
    initUI();
    init();
}

DlgLevel::~DlgLevel()
{
    delete ui;
}

void DlgLevel::initUI()
{
    //setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);

    m_settings->beginGroup("Servo");
    int level = m_settings->value("level").toInt();
    m_settings->endGroup();

    switch (level) {
    case 1:
        ui->rdoLevel1->setChecked(true);
        break;
    case 2:
        ui->rdoLevel2->setChecked(true);
        break;
    default:
        break;
    }

    connect(ui->btnOk,  &QPushButton::clicked, this, &DlgLevel::onOk);
}

void DlgLevel::init()
{
    // 测试级别
    m_settings->beginGroup("Servo");
    bool debug = m_settings->value("debug").toBool();
    m_settings->endGroup();

    QTimer::singleShot(100, this, [=]() {
        if (!debug) {
            emit prepare();
        } else {
            QMessageBox::StandardButton button = QMessageBox::question(this, QStringLiteral("提示"), QStringLiteral("当前运行的是调试版本,是否继续?"), QMessageBox::Yes | QMessageBox::No);
            if (button == QMessageBox::No)
                qApp->quit();
        }
    });
}

void DlgLevel::onOk()
{
    // 测试级别
    int level = 1;
    if (ui->rdoLevel2->isChecked())
        level = 2;

    m_settings->beginGroup("Servo");
    m_settings->setValue("level", level);
    m_settings->endGroup();

    this->accept();
}
